/*
 * @Author: szx
 * @Date: 2023-03-30 14:04:53
 * @LastEditTime: 2023-03-30 14:26:26
 * @Description: 
 * @FilePath: \learn\fullstackopen\part4\utils\list_helper.js
 */
const dummy = () => {
    return 1;
}

const totalLikes = (blogs) => {
    return blogs.reduce((a, b) => a + b.likes, 0)
}

const favoriteBlog = (blogs) => {
    let max = 0;
    let res = blogs[0]
    blogs.forEach(blog => {
        if (blog.likes > max) {
            max = blog.likes;
            res = blog;
        }
    });
    return res;
}

const mostBlogs = (blogs) => {
    const map = new Map();
    for (let b of blogs) map.set(b.author, (map.get(b.author) || 0) + 1)
    let res = { author: '', blogs: 0 };
    for (let m of map) {
        if (m[1] > res.blogs) {
            res.author = m[0];
            res.blogs = m[1];
        }
    }
    return res;
}
const mostLikes = (blogs) => {
    const map = new Map();
    for (let b of blogs) map.set(b.author, (map.get(b.author) || 0) + b.likes)
    let res = { author: '', likes: 0 };
    for (let m of map) {
        if (m[1] > res.likes) {
            res.author = m[0];
            res.likes = m[1];
        }
    }
    return res;
}
module.exports = {
    dummy,
    totalLikes,
    favoriteBlog,
    mostBlogs,
    mostLikes
}